package com.itheima.Demo;

import java.util.ArrayList;
import java.util.stream.Stream;

public class Demo非终结方法 {
    public static void main(String[] args) {
        //创建集合;
        ArrayList<String > list  = new ArrayList<>();
        //添加元素;
        list.add("柳岩");
        list.add("石原里美");
        list.add("新垣结衣");
        list.add("斋藤飞鸟");
        //通过集合获取流对象;
        Stream<String> stream = list.stream();
        //filter()  :过滤筛选
        //留下字符串长度为4的元素
        //如果返回true表示留下这个元素
        //如果返回false表示删除这个元素
        //stream.filter((s)->{return s.length()==4;}).forEach((s)->{System.out.println(s);
        //});
        //简化上面的代码;
        stream.filter(s->s.length()==4).forEach(s->System.out.println(s));
        System.out.println("================================================");
        //limit():   取用前几个
        //要留下流中的前三个元素
        Stream<String> stream1 = list.stream();
        stream1.limit(3).forEach(s-> System.out.println(s));
        System.out.println("================================================");
        //skip():   跳过前几个
        //跳过前两个元素
        Stream<String> stream2 = list.stream();
        stream2.skip(2).forEach(s-> System.out.println(s));
        System.out.println("================================================");
        //map() :  映射方法(把元素进行转换)
        //把流中的元素从【字符串】转成【字符串对应的长度】
        //方法参数代表的是转之前的元素
        //方法返回值代表的是转之后的元素
        Stream<String> stream3 = list.stream();
        stream3.map((s)->{return s.length();}).forEach(s-> System.out.println(s));
        System.out.println("================================================");
        //static concat()	:组合(把两个流拼接成一个流)
        Stream<String> stream4 = list.stream();
        Stream<String> skip = stream4.skip(3);

        Stream<String> stream5 = list.stream();
        Stream<String> limit = stream5.limit(2);
        Stream.concat(skip,limit).forEach(s-> System.out.println(s));

    }
}
